package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.ui.view.MyDialog;
import com.dtsz.m2.utils.RegluarUtils;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

import java.util.HashMap;
import java.util.Map;
public class RegisterTowActivity extends BaseActivity {
    private EditText etName, etCode, etQrcode, etCard;
    private Users users = new Users();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_two);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        etName = findViewById(R.id.et_name);
        etCode = findViewById(R.id.et_code);
        etQrcode = findViewById(R.id.et_qrcode);
        etCard = findViewById(R.id.et_card);
    }

    @Override
    public void initClick() {
        super.initClick();
        findViewById(R.id.tv_break).setOnClickListener(this);
        findViewById(R.id.bt_pwd).setOnClickListener(this);
    }

    MyDialog builder;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_break:
                finish();
                break;
            case R.id.bt_pwd:
                String code = etCode.getText().toString();
                String name = etName.getText().toString();
                String qrcode = etQrcode.getText().toString();
                String card = etCard.getText().toString();
                if (StringUtils.isEmpty(name)) {
                    showToast("姓名用于实名认证，不能为空");
                    return;
                }
                if (StringUtils.isEmpty(code)) {
                    showToast("朋友的邀请码，不能为空");
                    return;
                }
                if (StringUtils.isEmpty(qrcode)) {
                    showToast("自己的邀请码，不能为空");
                    return;
                }
                if (qrcode.length() <= 5) {
                    showToast("自己的邀请码，长度必须大于6位");
                    return;
                }
                boolean flagCode = RegluarUtils.isLetter(qrcode);
                if (!flagCode) {
                    Toast.makeText(this, "邀请码只能是数字或者字母", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (RegluarUtils.isLetterNumber(qrcode)) {
                    showToast("您设置的邀请码已被注册，请重新设置");
                    return;
                }
                if (!StringUtils.isIDNumber(card)) {
                    showToast("身份证号格式错误");
                    return;
                }
                String etMail = getSharedPreferences("etMail");
                String pwdEnd = getSharedPreferences("pwdEnd");




                users.setBuName(name);
                users.setPassword(pwdEnd);
                users.setBuMail(etMail);
                users.setPbucode(qrcode);
                users.setBucode(code);
                users.setBuCard(card);
                hucRequestInterfase.executeStr(this, String.class,
                        HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.REGISTER_URL,
                        hucParameInterfase.getRequestParameter(users), true);
//                View view = getLayoutInflater().inflate(R.layout.item_verification, null);
//                builder = new MyDialog(this, view, R.style.DialogTheme);
//                WebView webView = view.findViewById(R.id.bw_verification);
//                webView.clearCache(true);
//                webView.getSettings().setJavaScriptEnabled(true);
//                webView.setWebViewClient(new WebViewClient());
//                webView.addJavascriptInterface(new RegisterTowActivity.vaptchaInterface(), "vaptchaInterface");
//                webView.loadUrl(UrlConfig.URL_M2_DEV + "index.html");
//                builder.show();


                break;
        }
    }

    private void getClick() {
        loading();
        hucRequestInterfase.executeStr(this, String.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.REGISTER_URL,
                hucParameInterfase.getRequestParameter(users), true);
    }
    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                String msg = (String) response;
                if ("注册成功".equals(msg)) {
                    finish();
                    sendIntent(this, LoginActivity.class);
                }
                showToast(msg);
                break;
            case HUCState.HUC_REQEUST_CODE_3:
                String msg1 = response.toString();
                if ("成功".equals(msg1)) {
                    getClick();
                } else {
                    showToast(msg1);
                }
                break;
        }
        loadDismiss();
    }

    public class vaptchaInterface {
        @JavascriptInterface
        public void signal(String json, String token, String sign) {
            //json格式{signal:"",data:""}
            //signal: pass (通过) ； cancel（取消）
            try {
                runOnUiThread(() -> {
                    System.out.println(json);
                    System.out.println(token);
                    System.out.println(sign);
                    // Toast.makeText(RegisterActivity.this, "验证通过", Toast.LENGTH_SHORT).show();
                    Map<String, String> mapJson = new HashMap<>();
                    builder.dismiss();
                    mapJson.put("nc_token", token);
                    mapJson.put("csessionid", json);
                    mapJson.put("sig", sign);
                    mapJson.put("phone", etCard.getText().toString());
                    hucRequestInterfase.executeStr(RegisterTowActivity.this, String.class,
                            HUCState.HUC_REQEUST_CODE_3, Request.Method.POST, UrlConfig.FIT_AUTH_URL,
                            mapJson, true);

                    builder.dismiss();
                    // actionRegister();
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}